﻿using System;

namespace SingletonEvents
{
	internal class Singleton
	{
		#region Fields

		private static Singleton instance;
		private static readonly object syncRoot = new object();

		private int childFormCounter;
		private int activeChildFormNumber;

		#endregion Fields

		#region Properties

		public static Singleton Instance
		{
			get
			{
				if (instance == null)
				{
					lock (syncRoot)
					{
						if (instance == null)
						{
							instance = new Singleton();
						}
					}
				}

				return instance;
			}
		}

		public int ActiveChildFormNumber
		{
			get { return activeChildFormNumber; }
			set { activeChildFormNumber = value; }
		}

		#endregion Properties

		#region Events

		public event EventHandler<EventTriggeredEventsArgs> EventTriggered;

		#endregion Events

		#region Constructors

		private Singleton()
		{
			//
		}

		#endregion Constructors

		#region Methods

		public int GetChildFormNumber()
		{
			childFormCounter++;
			return childFormCounter;
		}

		public void TriggerEvent()
		{
			EventHandler<EventTriggeredEventsArgs> eventTriggered = EventTriggered;
			if (eventTriggered != null)
			{
				eventTriggered(this, new EventTriggeredEventsArgs(activeChildFormNumber));
			}
		}

		#endregion Methods
	}
}
